<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-upload" lay-event="upload">上传文件</button>
                <button class="layui-btn layui-btn-delete" lay-event="delete">删除</button>
            </div>
        </script>
    </div>
</div>

<div id="showViewUpload" style="display: none">
    <div class="layui-upload">
        <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
        <button type="button" class="layui-btn" id="test9">开始上传</button>
    </div>
</div>

<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload=layui.upload;

        table.render({
            elem: '#currentTableId',
            url: '/deploy/list',
            toolbar: '#toolbarDemo',
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            skin: 'line',
            page:true,
            cols: [[
                {type:'checkbox'},
                {field:'zizeng',  title: '序号',width: 80,align:'center',templet:function (d) {
                        return d.LAY_TABLE_INDEX+1;
                    }},
                {field: 'id',  title: '编号',align:'center'},
                {field: 'name',  title: '流程名称',align:'center', sort: true},
                {field: 'deploymentTime', title: '部署时间',align:'center',templet:function (d) {
                        return showTime(d.deploymentTime);
                    }}
            ]],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            }
        });

        //工具栏监听事件
        table.on('toolbar(currentTableFilter)', function(obj){
            switch (obj.event) {
                case 'upload':
                    layer.open({
                        type: 1,
                        title:'添加流程部署',
                        area: ['400px', '150px'], //自定义文本域宽高
                        content: $('#showViewUpload') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    var checkStatus=table.checkStatus('currentTableId');
                    var data=checkStatus.data;
                    if(data==""){
                        layer.msg('请选择一个',{icon:2});
                        return;
                    }
                    var ids="";
                    for(var i in data){
                        ids+=data[i].id
                    }
                    layer.confirm('是否删除？', {
                        btn: ['是', '否',],//可以无限个按钮
                    }, function(index, layero){
                        var dataIds={
                            'ids':ids
                        }
                        $.ajax({
                            type:"post",
                            dataType:"json",
                            url: '../../deploy/delete',//url
                            contentType: "application/json; charset=utf-8",
                            data:JSON.stringify(dataIds),
                            success: function (result) {
                                if(result.data.success==true){
                                    layer.msg("删除成功")
                                    layer.close(index);
                                }
                            },
                            error: function () {
                                layer.msg("操作失败");
                            }

                        })
                    });
                    break;

            }

        });

        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            ,url: '../../deploy/upload' //改成您自己的上传接口
            ,auto: false
            ,method:'GET'
            ,bindAction: '#test9'
            ,field:'deployFile'
            ,done: function(res){
                layer.msg('上传成功');
                console.log(res)
            }
        });

    });
    //时间转换函数
    function showTime(tempDate)
    {
        var d = new Date(tempDate);
        var year = d.getFullYear();
        var month = d.getMonth();
        month++;
        var day = d.getDate();
        var hours = d.getHours();

        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        month = month<10 ? "0"+month:month;
        day = day<10 ? "0"+day:day;
        hours = hours<10 ? "0"+hours:hours;
        minutes = minutes<10 ? "0"+minutes:minutes;
        seconds = seconds<10 ? "0"+seconds:seconds;


        var time = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        return time;
    }

</script>

</body>
</html>